<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="360px">
    <el-form ref="formItem" :model="formItem" label-width="100px" size="small" :rules="rules">
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择客户" prop="custId">
            <el-select v-model="formItem.custId" clearable filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteMethod">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.custId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="车牌号" prop="carNo">
            <el-input v-model="formItem.carNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="驾驶员" prop="driver">
            <el-input v-model="formItem.driver"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排放标准" prop="standardId">
            <el-select v-model="formItem.standardId" placeholder="请选择">
              <el-option label="国五" :value="1" />
              <el-option label="国六A" :value="2" />
              <el-option label="国六B" :value="3" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addStandard, updateStandard } from '@/api/standard'
import { getTenCust } from '@/api/cust'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      options: [],
      formItem: {
        standardId: 1,
        custId: '',
        carNo: '',
        driver: '',
      },
      rules: {
        carNo: [{ required: true, message: '请输入', trigger: 'blur' }],
        custId: [{ required: true, message: '请选择客户', trigger: 'blur' }],
      }
    }
  },
  methods: {
    init (row) {
      if (row) {
        this.title = '编辑'
        this.formItem = _.clone(row)
        this.isEdit = true
        this.options = [{ custId: row.custId, name: row.custName }]
        delete this.formItem.updateTime
      } else {
        this.title = '新增'
        this.formItem = {
          standardId: 1,
          custId: '',
          carNo: '',
          driver: '',
        }
        this.isEdit = false
        this.options = []
      }
      this.dialogVisible = true

    },
    async remoteMethod (keyword) {
      if (keyword) {
        const res = await getTenCust({ keyword })
        if (res.code === 200) {
          this.options = res.data
        } else {
          this.options = []
        }
      } else {
        this.options = [];
      }
    },
    async handleSubmit () {
      const res = this.isEdit ? await updateStandard(this.formItem) : await addStandard(this.formItem)
      if (res.code === 200) {
        this.$emit('ok')
        this.dialogVisible = false
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>

<style>
</style>